package com.yang.inventorymanagement.controllerview.users;

import cn.hutool.core.collection.EnumerationIter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yang.inventorymanagement.pojo.Suppliers;
import com.yang.inventorymanagement.pojo.User;
import com.yang.inventorymanagement.service.ProductsService;
import com.yang.inventorymanagement.service.UserService;
import com.yang.inventorymanagement.utils.SpringContextUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

@Lazy
@Component("UserAddSwing")
@Scope("prototype")
public class UserAddSwing extends JDialog {
    private JPanel contentPane;

    private JTextField userNameField;
    private JTextField passwordField;

    private JTextField contactEmailField;
    //商品表
    private UserService userService = SpringContextUtils.getBean(UserService.class);

    public UserAddSwing() {
        setModal(true);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        //设置窗口大小
        setBounds(100, 100, 573, 404);
        //设置居中显示
        setLocationRelativeTo(null);
        //设置不可编辑
        setResizable(false);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        Border titleBorder = BorderFactory.createTitledBorder("用户注册");
        contentPane.setBorder(titleBorder);
        setContentPane(contentPane);

        JPanel productsMsgPanel = new JPanel();
        productsMsgPanel.setBorder(BorderFactory.createTitledBorder("用户注册"));
        productsMsgPanel.setBounds(766, 54, 389, 661);
        add(productsMsgPanel);

// 设置布局管理器为GridBagLayout
        productsMsgPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.fill = GridBagConstraints.HORIZONTAL;

        JLabel lblid = new JLabel("用户名");
        productsMsgPanel.add(lblid, gbc);

        userNameField = new JTextField();
        userNameField.setVisible(true);
       // userNameField.setEnabled(false);
        userNameField.setColumns(10);
        productsMsgPanel.add(userNameField, gbc);

        JLabel label_3 = new JLabel("密码");
        productsMsgPanel.add(label_3, gbc);

        passwordField = new JTextField();
        passwordField.setVisible(true);
        //passwordField.setEnabled(false);
        productsMsgPanel.add(passwordField, gbc);




        /*JLabel jLabel = new JLabel("添加用户");
        jLabel.setBounds(210, 10, 142, 32);
        jLabel.setFont(new Font("宋体", Font.PLAIN, 24));


        JLabel suppliersNameLable = new JLabel("用户名：");
        suppliersNameLable.setBounds(136, 118, 80, 15);
        //用户输入框
        userNameField = new JTextField();
        //userNameField.setColumns(10);
        userNameField.setBounds(210, 115, 150, 21);
        contentPane.add(userNameField);

        JLabel contactName = new JLabel("密码：");
        contactName.setBounds(136, 148, 80, 15);
        contentPane.add(contactName);
        //用户输入框
        passwordField = new JTextField();
        //userNameField.setColumns(10);
        passwordField.setBounds(210, 145, 150, 21);
        contentPane.add(passwordField);*/


        JButton btnSubmit = new JButton("新增");
        productsMsgPanel.add(btnSubmit);

        //新增用户信息
        btnSubmit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (JOptionPane.showConfirmDialog(null, "确定新增？") == 0) {

                    //获取userId

                    String username = userNameField.getText();
                    String passwordFieldText = passwordField.getText();


                    //数据检查

                    if (username.equals("") || username.equals("")) {
                        JOptionPane.showMessageDialog(null, "填写错误，请重新填写", "提示", JOptionPane.ERROR_MESSAGE);
                        return;
                    }

                    QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
                    userQueryWrapper.eq("UserName", username);
                    User one = userService.getOne(userQueryWrapper);
                    if (one != null) {
                        JOptionPane.showMessageDialog(null, "用户已存在，请重新填写", "提示", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    //封装数据

                    User user = new User();
                    user.setUsername(username);
                    user.setPassword(passwordFieldText);    
                    int i = userService.insertUser(user);


                    //新增
                    if (i > 0) {
                        JOptionPane.showMessageDialog(null, "新增成功", "提示", JOptionPane.INFORMATION_MESSAGE);
                        // SessionUtils.getSession().put("user", userService.getById(userId));
                        setVisible(true);
                        dispose();
                    } else {
                        JOptionPane.showMessageDialog(null, "新增失败", "提示", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });
        btnSubmit.setBounds(230, 275, 93, 23);
        contentPane.add(btnSubmit);

    }
}
